package com.scu.phototabuser.util;

import android.support.annotation.NonNull;

import com.scu.phototabuser.enums.AppEnum;
import com.scu.phototabuser.exception.AppException;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;

/**
 * Created by tianfei on 2017/4/8.
 */

public final class Predictions {

    public static <T> T checkNotNull(T reference){
        if(reference == null) {
            throw new AppException("NullObjectException");
        } else {
            return reference;
        }
    }

    public static <T> T checkNotNull(T reference, @Nullable AppEnum error) {
        if(reference == null) {
            throw new AppException(error);
        } else {
            return reference;
        }
    }

    public static String getDataFromResponseJson(String json){
        try {
            JSONObject object=new JSONObject(json);
            return object.getString("data");
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static int[] getUserTypeArrayFromString(@NonNull String x){

        String[] dx=x.split(":");
        int[] ret=new int[dx.length];
        for (int i=0;i<dx.length;i++){
            ret[i]=Integer.parseInt(dx[i]);
        }

        return ret;
    }

    @NonNull
    public static String parseStringFromUserTypeArray(Integer[] a){

        StringBuilder b=new StringBuilder();
        for (int x:a){
            b.append(x);
            b.append(":");
        }
        return b.toString();
    }

}
